package symulatorwindy.animation;


import java.util.ArrayList;

import symulatorwindy.logic.Pietro;

/**
 *
 * @author Przemo
 */
public class ElevatorMovement{
    private ElevatorAnimation ea;

    Elevator first;
    Elevator second;

    Thread[] threads = new Thread[2];

    private int floor_count;
    private final int floor_size = ElevatorAnimation.FLOOR_SIZE;
    private int time_for_floor; // Tymczasowo /* Miliseconds */
    private int actual_floor;
    private double jump_time;
    private double speed;
    private int enter_exit_time;
    private boolean second_elevator;

    private int start1, start2;

    private ArrayList<Pietro> route_1;
    private ArrayList<Pietro> route_2;

    public void run(){
        first.setPietro(this.route_1);
        threads[0] = new Thread(first);
        threads[0].start();
        if(this.second_elevator){
            second.setPietro(this.route_2);
            threads[1] = new Thread(second);
            threads[1].start();
        }
    }

    public ElevatorMovement(int floor_count){
        this.start1 = start1;
        this.start2 = start2;
        this.floor_count = floor_count;
        this.init();
        this.speed = 1;
    }

    public void setStartFloors(int start1, int start2){
        this.start1 = start1;
        this.start2 = start2;
        this.init();
    }
    public ElevatorAnimation getElevatorAnimation(){
        return ea;
    }

    public void setPassangersOnFloorUp(int floor, int number){
        ea.floor_passengers_up[floor]=number;
    }

    public void setPassangersOnFloorsUp(int []passangers){
        if(passangers.length == ea.floor_passengers_up.length)
            ea.floor_passengers_up = passangers;
        else
            for(int i=0;i<passangers.length;i++)
                ea.floor_passengers_up[i] = passangers[i];
    }

    public void setPassangersOnFloorDown(int floor, int number){
        ea.floor_passengers_down[floor]=number;
    }

    public void setPassangersOnFloorsDown(int []passangers){
        if(passangers.length == ea.floor_passengers_down.length)
            ea.floor_passengers_down = passangers;
        else
            for(int i=0;i<passangers.length;i++)
                ea.floor_passengers_down[i] = passangers[i];
    }

    public void setPassangersInElevator(int passangers){
        System.out.println("setPassangersInElevator("+passangers+")");
        this.ea.elevator_passangers = passangers;
    }

    public int getPassangersInElevator(){
        return this.ea.elevator_passangers;
    }

    public int getPassangersOnFloorUp(int floor){
        return ea.floor_passengers_up[floor];
    }
    public int getPassangersOnFloorDown(int floor){
        return ea.floor_passengers_down[floor];
    }

    public void setTimeForFloor(int time){
        this.time_for_floor = time;
        this.jump_time = ((double)time)/((double)(this.floor_size));
        this.first.setTimeForFloor(time);
        if(this.second_elevator)
            this.second.setTimeForFloor(time);
    }

    public double getSpeed(){
        return this.speed;
    }

    public void setSpeed(int speed){
        this.speed = 1.0/speed;
        this.first.setSpeed(this.speed);
        if(this.second_elevator)
            this.second.setSpeed(this.speed);
    }

    public void setFloorsCount(int floors){
        this.floor_count = floors;
        this.first.setFloorsCount(floors);
        if(this.second_elevator)
            this.second.setFloorsCount(floors);
        this.init();
    }

    public void setEnterExitTime(int time){
        this.enter_exit_time = time;
        this.first.setEnterExitTime(time);
        if(this.second_elevator)
            this.second.setEnterExitTime(time);
    }

    private void init(){
        
        this.actual_floor = 0;
        this.ea = new ElevatorAnimation(this.floor_count, this.second_elevator);
        /* Start z parteru */
        this.ea.shift = this.floor_size * (this.floor_count-(start1+1));
        this.ea.shift2 = this.floor_size * (this.floor_count-(start2 + 1));
        this.jump_time = ((double)this.time_for_floor)/((double)(this.floor_size));
        //this.second_elevator = false;

        this.first = new Elevator(this.ea, this.floor_count, 1);
        this.second = new Elevator(this.ea, this.floor_count, 2);
        setTimeForFloor(1000);
    }

    public void setRoute(ArrayList pietro, int elevator){
        if(elevator == 1){
            this.route_1 = pietro;
            this.first.setPietro(pietro);
        }
        else{
            this.route_2 = pietro;
            this.second.setPietro(pietro);
        }
    }

    public void enableSecondElevator(){
        this.second = new Elevator(this.ea, this.floor_count, 2);
        this.second.setSpeed(this.speed);
        this.second_elevator = true;
        this.ea.repaint();
    }

    public void disableSecondElevator(){
        this.second_elevator = false;
        //this.second.interrupt();
        this.second = null;
        this.ea.repaint();
    }

    public void stop(){
        if(threads[0]!=null)threads[0].interrupt();
        if(threads[1]!=null)threads[1].interrupt();
    }
}
